package com.hao.study.controller;

import com.github.pagehelper.PageInfo;
import com.hao.study.common.APIResponse;
import com.hao.study.common.ResponeCode;
import com.hao.study.entity.Notice;
import com.hao.study.permission.Access;
import com.hao.study.permission.AccessLevel;
import com.hao.study.service.NoticeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author: author
 * @description:    公告
 * @date: 2024/6/12 22:15
 * @version: 1.0
 */
@Api(tags={"公告管理接口"})
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService service;

    @GetMapping("/list")
    public APIResponse list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String keyWord){
        PageInfo<Notice> list=service.getList(pageNum,pageSize,keyWord);
        return new APIResponse<>(ResponeCode.SUCCESS,"查询成功",list);
    }


    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/create")
    @Transactional
    public APIResponse create(Notice notice){
        service.createNotice(notice);
        return new APIResponse<>(ResponeCode.SUCCESS,"创建成功");
    }


    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/delete")
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteNotice(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/update")
    @Transactional
    public APIResponse update(Notice notice) throws IOException {
        service.updateNotice(notice);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }
}
